﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AutoAdaptWindowsSize.cs" company="HelloWorld">
//   BY田新宇
// </copyright>
// <summary>
// 实现窗体最大化自适应的方法
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Service
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    /// <summary>
    /// The auto adapt windows size.
    /// </summary>
    public class AutoAdaptWindowsSize
    {
        /// <summary>
        /// The controls info.
        /// </summary>
        private readonly Dictionary<string, string>
            controlsInfo = new Dictionary<string, string>(); // 控件中心Left,Top,控件Width,控件Height,控件字体Size

        /// <summary>
        /// The _form.
        /// </summary>
        private readonly Form form;

        /// <summary>
        /// The form original width.
        /// </summary>
        private double formOriginalWidth; // 窗体原始宽度

        /// <summary>
        /// The form original height.
        /// </summary>
        private double formOriginalHeight; // 窗体原始高度

        /// <summary>
        /// The scale x.
        /// </summary>
        private double scaleX; // 水平缩放比例

        /// <summary>
        /// The scale y.
        /// </summary>
        private double scaleY; // 垂直缩放比例

        /// <summary>
        /// Initializes a new instance of the <see cref="AutoAdaptWindowsSize"/> class.
        /// </summary>
        /// <param name="form">
        /// The form.
        /// </param>
        public AutoAdaptWindowsSize(Form form)
        {
            this.form = form;
        }

        /// <summary>
        /// The initialization controls info.
        /// </summary>
        /// <param name="ctrlContainer">
        /// The ctrl container.
        /// </param>
        public void InitControlsInfo(Control ctrlContainer)
        {
            if (ctrlContainer.Parent == this.form)
            {
                // 获取窗体的高度和宽度
                this.formOriginalWidth = Convert.ToDouble(ctrlContainer.Width);
                this.formOriginalHeight = Convert.ToDouble(ctrlContainer.Height);
            }

            foreach (Control item in ctrlContainer.Controls)
            {
                if (item.Name.Trim() != string.Empty)
                {
                    // 添加信息：键值：控件名，内容：据左边距离，距顶部距离，控件宽度，控件高度，控件字体。
                    this.controlsInfo.Add(item.Name, (item.Left + (item.Width / 2)) + "," + (item.Top + (item.Height / 2)) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
                }

                if ((item as UserControl) == null && item.Controls.Count > 0)
                {
                    this.InitControlsInfo(item);
                }
            }
        }

        /// <summary>
        /// The form size changed.
        /// </summary>
        public void FormSizeChanged()
        {
            if (this.controlsInfo.Count <= 0)
            {
                return;
            }

            this.ControlsZoomScale(this.form.Controls[0]); // 表示panel控件
            this.ControlsChange(this.form.Controls[0]);
        }

        /// <summary>
        /// The controls zoom scale.
        /// </summary>
        /// <param name="ctrlContainer">
        /// The ctrl container.
        /// </param>
        private void ControlsZoomScale(Control ctrlContainer)
        {
            this.scaleX = Convert.ToDouble(ctrlContainer.Width) / this.formOriginalWidth;
            this.scaleY = Convert.ToDouble(ctrlContainer.Height) / this.formOriginalHeight;
        }

        /// <summary>
        /// The controls change.
        /// </summary>
        /// <param name="ctrlContainer">
        /// The ctrl container.
        /// </param>
        private void ControlsChange(Control ctrlContainer)
        {
            var pos = new double[5]; // pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
            foreach (Control item in ctrlContainer.Controls)
            {
                // 遍历控件
                if (item.Name.Trim() == string.Empty)
                {
                    continue;
                }

                // 如果控件名不是空，则执行
                if (!(item is UserControl) && item.Controls.Count > 0)
                {
                    // 如果不是自定义控件
                    this.ControlsChange(item); // 循环执行
                }

                var strings = this.controlsInfo[item.Name].Split(','); // 从字典中查出的数据，以‘，’分割成字符串组

                for (var i = 0; i < 5; i++)
                {
                    pos[i] = Convert.ToDouble(strings[i]); // 添加到临时数组
                }

                var itemWidth = pos[2] * this.scaleX;     // 计算控件宽度，double类型
                var itemHeight = pos[3] * this.scaleY; // 计算控件高度
                item.Left = Convert.ToInt32((pos[0] * this.scaleX) - (itemWidth / 2)); // 计算控件距离左边距离
                item.Top = Convert.ToInt32((pos[1] * this.scaleY) - (itemHeight / 2)); // 计算控件距离顶部距离
                item.Width = Convert.ToInt32(itemWidth); // 控件宽度，int类型
                item.Height =
                    Convert.ToInt32(
                        itemHeight); // 控件高度  item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));//字体
            }
        }
    }
}